<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      :before-open="beforeOpen"
      :before-close="beforeClose"
      :upload-before="uploadBefore"
      :upload-after="uploadAfter"
      @row-del="rowDel"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      :page.sync="page"
      @on-load="onLoad"
    >
      <template v-slot:pastPapers="{ row }">
        <el-button
          size="mini"
          type="primary"
          @click="downloadFile(row.pastPapersDownload)"
          icon="el-icon-download"
          >下载</el-button
        >
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getCerList,
  getCerDetail,
  removeCer,
  addCer,
  updateCer
} from '@/api/certificates/index.js'
import { baseUrl } from '@/api/baseUrl'
export default {
  data() {
    return {
      data: [],
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option: {
        tip: false,
        simplePage: true,
        searchShow: true,
        searchMenuSpan: 18,
        searchMenuPosition: 'right',
        tree: true,
        border: true,
        addBtn: false,
        index: true,
        // selection: true,
        editBtn: true,
        delBtn: false,
        viewBtn: true,
        menuWidth: 300,
        dialogClickModal: false,
        height: 700,
        addBtn: true,
        delBtn: true,
        labelWidth: 120,
        column: [
          {
            label: '标题',
            prop: 'title',
            maxlength: 20,
            searchLabelWidth: 60,
            showWordLimit: true,
            search: true,
            width: 150,
            rules: [
              {
                required: true,
                message: '请输入标题',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '考试介绍',
            prop: 'examIntroduction',
            span: 24,
            type: 'textarea',
            maxlength: 100,
            showWordLimit: true,
            rules: [
              {
                required: true,
                message: '请输入考试介绍',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '报名时间',
            prop: 'registrationTime',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            defaultTime: ['00:00:00', '23:59:59'],
            type: 'datetimerange',
            overHidden: true,
            rules: [
              {
                required: true,
                message: '请选择报名时间',
                trigger: 'change'
              }
            ]
          },
          {
            label: '考试日程',
            prop: 'examSchedule',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            type: 'datetimerange',
            overHidden: true,
            rules: [
              {
                required: true,
                message: '请选择考试日程',
                trigger: 'change'
              }
            ]
          },
          {
            label: '考试大纲',
            prop: 'examSyllabus',
            type: 'textarea',
            span: 24,
            maxlength: 100,
            showWordLimit: true,
            rules: [
              {
                required: true,
                message: '请输入考试大纲',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '医考百问内容',
            prop: 'medicalExamQa',
            type: 'textarea',
            span: 24,
            maxlength: 100,
            showWordLimit: true,
            rules: [
              {
                required: true,
                message: '请输入医考百问内容',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '真题下载链接',
            prop: 'pastPapers',
            type: 'upload',
            width: 150,
            display: false
          },
          {
            label: '真题下载链接',
            prop: 'pastPapersDownload',
            hide: true,
            type: 'upload',
            listType: 'text',
            loadText: '模板上传中，请稍等',
            span: 24,
            limit: 1,
            dataType: 'string',
            propsHttp: {
              home: 'http://localhost:3000/',
              res: 'data',
              name: 'filename',
              url: 'fileUrl'
            },
            tip: '请上传 xls/xlsx,pdf,doc/docx格式附件',
            uploadPreview(file, column, done) {
              this.$preView(file.url)
            },
            action: `${baseUrl}/upload/file`,
            rules: [
              {
                required: true,
                message: '请上传附件',
                trigger: 'change'
              }
            ]
          }
        ]
      }
    }
  },
  components: {},
  created() {},
  mounted() {},
  computed: {},
  watch: {},
  methods: {
    downloadFile(url) {
      this.$preView('http://localhost:3000/' + url)
    },
    uploadAfter(res, done) {
      console.log(res, '????????????')
      // this.form.pastPapersDownload = res.data.fileUrl
      done()
    },
    setParams(row) {
      let {
        title,
        examIntroduction,
        registrationTime,
        examSchedule,
        examSyllabus,
        medicalExamQa,
        pastPapersDownload
      } = row
      const regTime = JSON.stringify(registrationTime)
      const examTime = JSON.stringify(examSchedule)
      // const files = JSON.stringify(pastPapersDownload)
      const params = {
        title,
        examIntroduction,
        registrationTime: regTime,
        examSchedule: examTime,
        examSyllabus,
        medicalExamQa,
        pastPapersDownload
      }
      return params
    },
    rowSave(row, done, loading) {
      const params = this.setParams(row)
      console.log(params)
      addCer(params).then(
        () => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        (error) => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowUpdate(row, index, done, loading) {
      const params = this.setParams(row)
      console.log(params)
      updateCer({ ...params, id: row.id }).then(
        (res) => {
          console.log(res, '???????????')
          if (res.data.code == 200) {
            this.onLoad(this.page)
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            })
          }
          done()
        },
        (error) => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowDel(row) {
      this.$confirm('确定将选择编号删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeCer(row.id)
        })
        .then(() => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    },
    onLoad(page, params = {}) {
      this.loading = true
      getCerList(page.currentPage, page.pageSize, {
        ...params
      }).then((res) => {
        const data = res.data.data
        this.page.total = res.data.total
        this.data = data.map((item) => {
          item.registrationTime = JSON.parse(item.registrationTime)
          item.examSchedule = JSON.parse(item.examSchedule)
          // item.pastPapersDownload = JSON.parse(item.pastPapersDownload)
          return item
        })
        console.log(res, this.data, res.data, '...............')
        this.loading = false
      })
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage
      // this.onLoad(this.page)
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    refreshChange() {
      this.onLoad(this.page, this.query)
    },
    searchReset() {
      this.query = {}
      this.onLoad(this.page)
    },
    searchChange(params, done) {
      this.query = params
      this.page.currentPage = 1
      this.onLoad(this.page, this.query)
      done()
    }
  }
}
</script>
